package com.atguigu.eduservice.controller;


import com.atguigu.common.R;
import com.atguigu.common.exception.GuliException;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.teachervo.QueryTeacher;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-06
 */
@Api(description="讲师管理")
@RestController  //jackson
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    //调用service，注入service
    @Autowired
    private EduTeacherService teacherService;

    //1 查询所有讲师
    // http://localhost:8001/eduservice/teacher/findAll
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
//    @RequestMapping(value = "findAll",method = RequestMethod.GET)
    public R getList() {
        //调用service
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //2 逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R deleteTeacher(@PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if(flag) {
            //成功
            return R.ok();
        } else {
            return R.error();
        }
    }

    //3 分页查询讲师
    //current：当前页  limit：每页显示记录数
    @GetMapping("findPageTeacher/{current}/{limit}")
    public R findPageTeacher(@PathVariable long current,@PathVariable long limit) {



        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //调用方法
        //mp封装：把分页所有数据封装到pageTeacher对象里面
        teacherService.page(pageTeacher,null);

        //pageTeacher对象获取分页数据
        long total = pageTeacher.getTotal(); //总记录数
        List<EduTeacher> list = pageTeacher.getRecords(); //数据list集合

//        Map<String, Object> data = new HashMap<>();
//        data.put("total",total);
//        data.put("rows",list);
//        return R.ok().data(data);

        return R.ok().data("total",total).data("rows",list);
    }

    //4 条件查询带分页
    //current：当前页  limit：每页显示记录数
    @ApiOperation(value = "条件查询带分页")
    @PostMapping ("findConditionPageTeacher/{current}/{limit}")
    public R findConditionPageTeacher(@PathVariable long current,
                                      @PathVariable long limit,
                                      @RequestBody QueryTeacher queryTeacher) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //获取条件值

            String name = queryTeacher.getName(); //讲师名称
            Integer level = queryTeacher.getLevel(); //头衔
            String begin = queryTeacher.getBegin(); //开始
            String end = queryTeacher.getEnd(); //结束

        //判断条件值是否为空，如果不为空，封装条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)) {  //null  ""
            //封装条件
            wrapper.like("name",name);
        }
        if(level==1||level==2) {
            //封装条件
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            //封装条件
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            //封装条件
            wrapper.le("gmt_create",begin);
        }

        //调用方法实现条件查询带分页
        teacherService.page(pageTeacher,wrapper);

        //pageTeacher对象获取分页数据
        long total = pageTeacher.getTotal(); //总记录数
        List<EduTeacher> list = pageTeacher.getRecords(); //数据list集合

        return R.ok().data("total",total).data("rows",list);
    }

    //5添加讲师的方法
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        return R.ok();
    }

    //6 根据讲师id查询
    @ApiOperation(value = "查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    //7  修改讲师的方法
    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        teacherService.updateById(eduTeacher);
        return R.ok();
    }
}

